﻿#region License
/* ****************************************
 * Allrights reserved by BlueLotus7 
 * 
 * Last updated: 2009-10-01
 * 
 * BlueLotus7@126.com
 * BlueLotus7@vip.qq.com
 * ****************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using BlueLotus7.Common;
using System.Runtime.Serialization;

namespace BlueLotus7.Contract
{
    public abstract class BaseExternalDelegateAccount : BaseExternalAccount
    {

        #region Fields

        [DataMember]
        protected string tokenKey;
        [DataMember]
        protected bool hasLogin;
        [DataMember]
        protected DateTime? loginedStamp;
        [DataMember]
        protected DateTime? expiredStamp;
        [DataMember]
        protected TimeSpan? expiredOffset;

        #endregion

        #region Property

        /// <summary>
        /// The account id for login.
        /// </summary>
        public string LoginId
        {
            get;
            set;
        }

        /// <summary>
        /// Indicates has login or not.
        /// </summary>
        public bool HasLogin
        {
            get { return hasLogin; }
        }

        /// <summary>
        /// The logined stamp.
        /// </summary>
        public DateTime? LoginedStamp
        {
            get { return loginedStamp; }
        }

        /// <summary>
        /// Indecates has expired or not.
        /// </summary>
        public bool HasExpired
        {
            get
            {
                if (expiredStamp != null && expiredStamp < DateTime.Now)
                {
                }
                return false;
            }
        }

        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="ownerKey">The owner key.</param>
        /// <param name="loginId">The login ID.</param>
        /// <param name="loginPassowrd">The login password</param>
        public BaseExternalDelegateAccount(Guid ownerKey, string loginId, string loginPassowrd) :
            base(ownerKey, loginId)
        {
            this.Password = loginPassowrd;
        }

        /// <summary>
        /// Sets status to logined.
        /// </summary>
        public virtual void SetLogined()
        {
            hasLogin = true;
            loginedStamp = DateTime.Now;
            expiredStamp = loginedStamp + expiredOffset;
        }

        /// <summary>
        /// Clears login info.
        /// </summary>
        public virtual void ClearLoginInfo()
        {
            hasLogin = false;
            loginedStamp = null;
            expiredStamp = null;
            tokenKey = null;
        }

        /// <summary>
        /// Refreshes expired stamp.
        /// </summary>
        public virtual void RefreshExpiredStamp()
        {
            if (expiredOffset != null && expiredOffset > Constant.ConstantDatas.ZeroTimeSpan)
            {
                expiredStamp = DateTime.Now + expiredOffset.Value;
            }
        }
    }
}
